<?
include_once('../conn.php');
$site = 'http://www.parismemo.com/shop/';


function authcode($string, $operation = 'DECODE', $key = '159357456plmqaz', $expiry = 0)
{
    $ckey_length = 4;

    $key = md5($key ? $key : 'holdyes');
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
    $cryptkey = $keya.md5($keya.$keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if($operation == 'DECODE') {
        if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc.str_replace('=', '', base64_encode($result));
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hold Yes Zencart Module</title>
<style>
.btn1{
	margin:5px; border:solid #CCC 1px; padding:5px;}</style>
</head>

<body>
<?
include_once('../include/encryption.php');
eval(@encryption('732d0n49Duv+ky95xAXs1Oz3Qf2XvfHjodb93OtpaOG/SHk9NvrOTNUWXa+eThilotpK6yzWIU7ASTORRYU04i4auCr2+ZvA8SatXcBz7ZJViFgt6mEPW4Ivlf5p7NHqODGIabHLzmBZMBQmJIohzedThNecF6eNT3iHNgdyVP6t/Za+am8RbhhQwPWLMypgoEax/YHwEKePkHnB+zk+X/Jk8G78l9XX8Hl8QxL6USE4aDeLSSXXunA5BmbwaZDZwWyDQqidw1on5nwUPEEZMG1OB/q6sa1Y','DECODE'));


?>
<div style="margin:10px 0;">
 <a href="?action=1" class="btn1" onclick="return confirm('确定更新吗？如果您是第一次更新将会删除原有目录。如果已经同步更新过，则会清除不存在目录！')">更新目录</a> | <a href="?action=2" class="btn1" onclick="return confirm('确定更新吗？如果您是第一次更新将会删除原有商品。如果已经同步更新过，则会清除不存在数据！')">更新商品</a></div>
 


<?


if($_GET[action]==1){
	if(!ini_get('safe_mode') ){
    set_time_limit(0);
}
 
 $arr = file_get_contents($site.'holdyes_zencart.php?action=1');
$arr = authcode($arr,'DECODE',$get_key);

eval("\$str = ".$arr);
if (is_array($str) === false) {
    exit('false returned');
}
foreach($str as $key => $value){
	$check = $db->select("SELECT other_shop_id FROM `".tablepre."stock_type` where other_shop_id=".floatval($key)."");
	if($check[0][0] == FALSE){
		$arr2=explode('=+=',$value);
		$db->INSERT("INSERT INTO `".tablepre."stock_type` (`tid`,`name`,`father`,`other_shop_id`,`rank`) VALUES ('".floatval($key)."','".htmlspecialchars($arr2[1],ENT_QUOTES, "UTF-8")."','".floatval($arr2[0])."', '".floatval($key)."','".floatval($arr2[2])."')");
		}else{
			$arr2=explode('=+=',$value);
			//$db->DELETE("DELETE FROM `".tablepre."stock_type` WHERE (`other_shop_id`='".floatval($key)."')");
			//echo '('.htmlspecialchars($arr2[1],ENT_QUOTES, "UTF-8").')已经存在。ID'.floatval($key).'<br />';
			}
	}				$check_exist = $db->select("SELECT other_shop_id FROM `".tablepre."stock_type`");
				for($i=0;$i<count($check_exist);$i++) {
					if(!array_key_exists($check_exist[$i][0],$str)){
						$db->DELETE("DELETE FROM `".tablepre."stock_type` WHERE (`other_shop_id`='".$check_exist[$i][0]."')");
						}
				}
echo '<br />完成';
}elseif($_GET[action]==2){
	if(!ini_get('safe_mode') ){
    set_time_limit(0);
}
//支付安全模式

	$arr = file_get_contents($site.'holdyes_zencart.php?action=2');
	//获取内容
	$arr = authcode($arr,'DECODE',$get_key);
	eval("\$str = ".$arr);
	//转换
if (is_array($str) === false) {
    exit('false returned');
}

$check_now_id = $db->select("SELECT sid FROM `".tablepre."stock_attachment` where `type` = 'pic'");
for($i=0;$i<count($check_now_id);$i++) {
	//echo 'id'.$check_now_id[$i][0].'=='.$str[$check_now_id[$i][0]];
	$getpic=explode('=+=',$str[$check_now_id[$i][0]]);
	$db->UPDATE("UPDATE `".tablepre."stock_attachment` SET `value1`='".$site.'images/'.$getpic[3]."' WHERE (`sid`='".$check_now_id[$i][0]."' and `type` = 'pic')");
	
	}
//更新图片
	foreach($str as $key => $value){
		$check = $db->select("SELECT other_shop_id FROM `".tablepre."stock` where other_shop_id=".floatval($key)."");
		$check_item = $db->select("SELECT sid FROM `".tablepre."stock_item` where sid=".floatval($key)."");
		if($check_item[0][0]==null){
			$db->INSERT("INSERT INTO `".tablepre."stock_item` (`sid`,`quantity`) VALUES ('".floatval($key)."','0')");
			}
		//检查是否已经有记录 
		if($check[0][0] == FALSE){
			//没有记录
			$arr2=explode('=+=',$value);
			$db->INSERT("INSERT INTO `".tablepre."stock` (`sid`,`name`,`tid`,`delete`,`onsale`,`other_shop_id`) VALUES ('".floatval($key)."','".htmlspecialchars($arr2[2],ENT_QUOTES, "UTF-8")."','".floatval($arr2[0])."', '0','0','".floatval($key)."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('model','".floatval($key)."', '".$arr2[1]."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('pic','".floatval($key)."', '".$site.'images/'.$arr2[3]."')");
			$db->INSERT("INSERT INTO `".tablepre."stock_attachment` (`type`,`sid`,`value1`) VALUES ('url','".floatval($key)."', '".$site.'index.php?main_page=product_info&products_id='.floatval($key)."')");
			//写入
			}else{
				//有记录
				$arr2=explode('=+=',$value);
				}

		}
				$check_exist = $db->select("SELECT other_shop_id FROM `".tablepre."stock`");
				for($i=0;$i<count($check_exist);$i++) {
					if(!array_key_exists($check_exist[$i][0],$str)){
						//如果不存在就删除
						$db->DELETE("DELETE FROM `".tablepre."stock` WHERE (`other_shop_id`='".$check_exist[$i][0]."')");
						$db->DELETE("DELETE FROM `".tablepre."stock_attachment` WHERE (`sid`='".$check_exist[$i][0]."')");
						}
				}
				$arr_to = file_get_contents($site.'holdyes_zencart.php?action=3');
				$arr_to = authcode($arr_to,'DECODE',$get_key);

eval("\$str_to = ".$arr_to);
if (is_array($str_to) === false) {
    exit('false returned');
}
				$db->DELETE("DELETE FROM `".tablepre."to_categories`");
				foreach($str_to as $key => $value){
					$db->INSERT("INSERT INTO `".tablepre."to_categories` (`products_id`,`categories_id`) VALUES ('".floatval($key)."','".floatval($value)."')");
					}
				
		echo '<br />完成';
	}
?>
</body>
</html>
